home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MEMORY.SWG / 0034_EMS Unit.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  14KB  |  444 lines

  1. {
  2. From: CYRUS PATEL
  3. Subj: EMS for BP
  4. }
  5.  
  6. program Ems_Test;
  7. { *************************************************************
  8. * This program shows you how to use the basic functions of  *
  9. * the LIM Expanded Memory Specification. Since it does not  *
  10. * use any of the LIM EMS 4.0 function calls, you can also   *
  11. * use it on systems with EMS versions less than 4.0         *
  12. ************************************************************* }
  13.  
  14. { Written by:
  15. Peter Immarco.
  16. Thought Dynamics
  17. Manhattan Beach, CA
  18. Compuserve ID# 73770,123
  19. *** Public Domain ***
  20.  
  21. Used by permission of the author.
  22. }
  23.  
  24. { This program does the following:
  25. +------------------------------------------------------------+
  26. | * Makes sure the LIM Expanded Memory Manager (EMM) has     |
  27. |   been installed in memory                                 |
  28. | * Displays the version number of the EMM present in memory |
  29. | * Determines if there are enough pages (16k blocks) of     |
  30. |   memory for our test program's usage. It then displays    |
  31. |   the total number of EMS pages present in the system,     |
  32. |   and how many are available for our usage                 |
  33. | * Requests the desired number of pages from the EMM        |
  34. | * Maps a logical page onto one of the physical pages given |
  35. |   to us                                                    |
  36. | * Displays the base address of our EMS memory page frame   |
  37. | * Performs a simple read/write test on the EMS memory given|
  38. |   to us                                                    |
  39. | * Returns the EMS memory given to us back to the EMM, and  |
  40. |   exits                                                    |
  41. +------------------------------------------------------------|}
  42.  
  43.  
  44. { All the calls are structured to return the result or error
  45. code of the Expanded Memory function performed as an integer.
  46. If the error code is not zero, which means the call failed,
  47. a simple error procedure is called and the program terminates.}
  48.  
  49. uses Crt, Dos;
  50.  
  51. Type
  52. ST3  = string[3];
  53. ST80 = string[80];
  54. ST5 = string[5];
  55.  
  56. Const
  57. EMM_INT                   = $67;
  58. DOS_Int                   = $21;
  59. GET_PAGE_FRAME            = $41;
  60. GET_UNALLOCATED_PAGE_COUNT= $42;
  61. ALLOCATE_PAGES            = $43;
  62. MAP_PAGES                 = $44;
  63. DEALLOCATE_PAGES          = $45;
  64. GET_VERSION               = $46;
  65.  
  66. STATUS_OK                 = 0;
  67.  
  68. { We'll say we need 1 EMS page for our application }
  69. APPLICATION_PAGE_COUNT    = 1;
  70.  
  71. Var
  72. Regs: Registers;
  73. Emm_Handle,
  74. Page_Frame_Base_Address,
  75. Pages_Needed,
  76. Physical_Page,
  77. Logical_Page,
  78. Offset,
  79. Error_Code,
  80. Pages_EMS_Available,
  81. Total_EMS_Pages,
  82. Available_EMS_Pages: Word;
  83. Version_Number,
  84. Pages_Number_String: ST3;
  85. Verify: Boolean;
  86.  
  87. { * --------------------------------------------------------- * }
  88. { The function Hex_String converts an Word into a four
  89. character hexadecimal number(string) with leading zeroes.   }
  90. Function Hex_String(Number: Word): ST5;
  91. Function Hex_Char(Number: Word): Char;
  92. Begin
  93. If Number<10 then
  94. Hex_Char:=Char(Number+48)
  95. else
  96. Hex_Char:=Char(Number+55);
  97. end; { Function Hex_Char }
  98.  
  99. Var
  100. S: ST5;
  101. Begin
  102. S:='';
  103. S:=Hex_Char( (Number shr 1) div 2048);
  104. Number:=( ((Number shr 1) mod 2048) shl 1)+
  105. (Number and 1) ;
  106. S:=S+Hex_Char(Number div 256);
  107. Number:=Number mod 256;
  108. S:=S+Hex_Char(Number div 16);
  109. Number:=Number mod 16;
  110. S:=S+Hex_Char(Number);
  111. Hex_String:=S+'h';
  112. end; { Function Hex_String }
  113.  
  114. { * --------------------------------------------------------- * }
  115.  
  116. { The function Emm_Installed checks to see if the Expanded
  117. Memory Manager (EMM) is loaded in memory. It does this by
  118. looking for the string 'EMMXXXX0', which should be located
  119. at 10 bytes from the beginning of the code segment pointed
  120. to by the EMM interrupt, 67h                                }
  121. Function Emm_Installed: Boolean;
  122. Var
  123. Emm_Device_Name       : string[8];
  124. Int_67_Device_Name    : string[8];
  125. Position              : Word;
  126. Regs                  : registers;
  127.  
  128. Begin
  129. Int_67_Device_Name:='';
  130. Emm_Device_Name   :='EMMXXXX0';
  131. with Regs do
  132. Begin
  133. { Get the code segment pointed to by Interrupt 67h, the EMM
  134. interrupt by using DOS call $35, 'get interrupt vector'     }
  135. AH:=$35;
  136. AL:=EMM_INT;
  137. Intr(DOS_int,Regs);
  138.  
  139. { The ES pseudo-register contains the segment address pointed
  140. to by Interrupt 67h }
  141. { Create an 8 character string from the 8 successive bytes
  142. pointed to by ES:$0A (10 bytes from ES)                   }
  143. For Position:=0 to 7 do
  144. Int_67_Device_Name:=
  145. Int_67_Device_Name+Chr(mem[ES:Position+$0A]);
  146. Emm_Installed:=True;
  147. { Is it the EMM manager signature, 'EMMXXXX0'? then EMM is
  148. installed and ready for use, if not, then the EMM manager
  149. is not present                                            }
  150. If Int_67_Device_Name<>Emm_Device_Name
  151. then Emm_Installed:=False;
  152. end; { with Regs do }
  153. end;  { Function Emm_Installed }
  154.  
  155. { * --------------------------------------------------------- * }
  156.  
  157. { This function returns the total number of EMS pages present
  158. in the system, and the number of EMS pages that are
  159. available for our use                                       }
  160. Function EMS_Pages_Available
  161. (Var Total_EMS_Pages,Pages_Available: Word): Word;
  162. Var
  163. Regs: Registers;
  164. Begin
  165. with Regs do
  166. Begin
  167. { Put the desired EMS function number in the AH pseudo-
  168. register                                                }
  169. AH:=Get_Unallocated_Page_Count;
  170. intr(EMM_INT,Regs);
  171. { The number of EMS pages available is returned in BX     }
  172. Pages_Available:=BX;
  173. { The total number of pages present in the system is
  174. returned in DX                                          }
  175. Total_EMS_Pages:=DX;
  176. { Return the error code                                   }
  177. EMS_Pages_Available:=AH
  178. end;
  179. end; { EMS_Pages_Available }
  180.  
  181. { * --------------------------------------------------------- * }
  182.  
  183. { This function requests the desired number of pages from the
  184. EMM                                                         }
  185. Function Allocate_Expanded_Memory_Pages
  186. (Pages_Needed: Word; Var Handle: Word   ): Word;
  187. Var
  188. Regs: Registers;
  189. Begin
  190. with Regs do
  191. Begin
  192. { Put the desired EMS function number in the AH pseudo-
  193. register                                                }
  194. AH:= Allocate_Pages;
  195. { Put the desired number of pages in BX                   }
  196. BX:=Pages_Needed;
  197. intr(EMM_INT,Regs);
  198. { Our EMS handle is returned in DX                        }
  199. Handle:=DX;
  200. { Return the error code }
  201. Allocate_Expanded_Memory_Pages:=AH;
  202. end;
  203. end; { Function Allocate_Expanded_Memory_Pages }
  204.  
  205. { * --------------------------------------------------------- * }
  206.  
  207. { This function maps a logical page onto one of the physical
  208. pages made available to us by the
  209. Allocate_Expanded_Memory_Pages function                     }
  210. Function Map_Expanded_Memory_Pages
  211. (Handle,Logical_Page,Physical_Page: Word): Word;
  212. Var
  213. Regs: Registers;
  214. Begin
  215. with Regs do
  216. Begin
  217. { Put the desired EMS function number in the AH pseudo-
  218. register                                                }
  219. AH:=Map_Pages;
  220. { Put the physical page number to be mapped into AL       }
  221. AL:=Physical_Page;
  222. { Put the logical page number to be mapped in    BX       }
  223. BX:=Logical_Page;
  224. { Put the EMS handle assigned to us earlier in   DX       }
  225. DX:=Handle;
  226. Intr(EMM_INT,Regs);
  227. { Return the error code }
  228. Map_Expanded_Memory_Pages:=AH;
  229. end; { with Regs do }
  230. end; { Function Map_Expanded_Memory_Pages }
  231.  
  232. { * --------------------------------------------------------- * }
  233.  
  234. { This function gets the physical address of the EMS page
  235. frame we are using. The address returned is the segment
  236. of the page frame.                                          }
  237. Function Get_Page_Frame_Base_Address
  238. (Var Page_Frame_Address: Word): Word;
  239. Var
  240. Regs: Registers;
  241. Begin
  242. with Regs do
  243. Begin
  244. { Put the desired EMS function number in the AH pseudo-
  245. register                                                }
  246. AH:=Get_Page_Frame;
  247. intr(EMM_INT,Regs);
  248. { The page frame base address is returned in BX           }
  249. Page_Frame_Address:=BX;
  250. { Return the error code }
  251. Get_Page_Frame_Base_Address:=AH;
  252. end; { Regs }
  253. end; { Function Get_Page_Frame_Base_Address }
  254.  
  255. { * --------------------------------------------------------- * }
  256.  
  257. { This function releases the EMS memory pages allocated to
  258. us, back to the EMS memory pool.                            }
  259. Function Deallocate_Expanded_Memory_Pages
  260. (Handle: Word): Word;
  261. Var
  262. Regs: Registers;
  263. Begin
  264. with Regs do
  265. Begin
  266. { Put the desired EMS function number in the AH pseudo-register }
  267. AH:=DEALLOCATE_PAGES;
  268. { Put the EMS handle assigned to our EMS memory pages in DX }
  269. DX:=Emm_Handle;
  270. Intr(EMM_INT,Regs);
  271. { Return the error code }
  272. Deallocate_Expanded_Memory_Pages:=AH;
  273. end; { with Regs do }
  274. end;  { Function Deallocate_Expanded_Memory_Pages }
  275.  
  276. { * --------------------------------------------------------- * }
  277.  
  278. { This function returns the version number of the EMM as
  279. a 3 character string.                                       }
  280. Function Get_Version_Number(Var Version_String: ST3): Word;
  281. Var
  282. Regs: Registers;
  283. Word_Part,Fractional_Part: Char;
  284.  
  285. Begin
  286. with Regs do
  287. Begin
  288. { Put the desired EMS function number in the AH pseudo-register }
  289. AH:=GET_VERSION;
  290. Intr(EMM_INT,Regs);
  291. { See if call was successful }
  292. If AH=STATUS_OK then
  293. Begin
  294. { The upper four bits of AH are the Word portion of the
  295. version number, the lower four bits are the fractional
  296. portion. Convert the Word value to ASCII by adding 48. }
  297. Word_Part   := Char( AL shr 4 + 48);
  298. Fractional_Part:= Char( AL and $F +48);
  299. Version_String:= Word_Part+'.'+Fractional_Part;
  300. end; { If AH=STATUS_OK }
  301. { Return the function calls error code }
  302. Get_Version_Number:=AH;
  303. end; { with Regs do }
  304. end; { Function Get_Version_Number }
  305.  
  306. { * --------------------------------------------------------- * }
  307.  
  308. { This procedure prints an error message passed by the caller,
  309. prints the error code passed by the caller in hex, and then
  310. terminates the program with the an error level of 1         }
  311.  
  312. Procedure Error(Error_Message: ST80; Error_Number: Word);
  313. Begin
  314. Writeln(Error_Message);
  315. Writeln('  Error_Number = ',Hex_String(Error_Number) );
  316. Writeln('EMS test program aborting.');
  317. Halt(1);
  318. end; { Procedure Error_Message }
  319.  
  320. { * --------------------------------------------------------- * }
  321.  
  322. { EMS_TEST }
  323.  
  324. { This program is an example of the basic EMS functions that you
  325. need to execute in order to use EMS memory with Turbo Pascal  }
  326.  
  327. Begin
  328. ClrScr;
  329. Window(5,2,77,22);
  330.  
  331. { Determine if the Expanded Memory Manager is installed, If
  332. not, then terminate 'main' with an ErrorLevel code of 1. }
  333.  
  334. If not (Emm_Installed) then
  335. Begin
  336. Writeln('The LIM Expanded Memory Manager is not installed.');
  337. Halt(1);
  338. end;
  339.  
  340. { Get the version number and display it }
  341. Error_Code:= Get_Version_Number(Version_Number);
  342. If Error_Code<>STATUS_OK then
  343. Error('Error trying to get the EMS version number ',
  344. Error_code)
  345. else
  346. Writeln('LIM Expanded Memory Manager, version ',
  347. Version_Number,' is ready for use.');
  348. Writeln;
  349.  
  350. { Determine if there are enough expanded memory pages for this
  351. application. }
  352. Pages_Needed:=APPLICATION_PAGE_COUNT;
  353. Error_Code:=
  354. EMS_Pages_Available(Total_EMS_Pages,Available_EMS_Pages);
  355. If Error_Code<>STATUS_OK then
  356. Error('Error trying to determine the number of EMS pages available.',
  357. Error_code);
  358.  
  359. Writeln('There are a total of ',Total_EMS_Pages,
  360. ' expanded memory pages present in this system.');
  361. Writeln('  ',Available_EMS_Pages,
  362. ' of those pages are available for your usage.');
  363. Writeln;
  364.  
  365. { If there is an insufficient number of pages for our application,
  366. then report the error and terminate the EMS test program }
  367. If Pages_Needed>Available_EMS_Pages then
  368. Begin
  369. Str(Pages_Needed,Pages_Number_String);
  370. Error('We need '+Pages_Number_String+
  371. ' EMS pages. There are not that many available.',
  372. Error_Code);
  373. end; { Pages_Needed>Available_EMS_Pages }
  374.  
  375. { Allocate expanded memory pages for our usage }
  376. Error_Code:= Allocate_Expanded_Memory_Pages(Pages_Needed,Emm_Handle);
  377. Str(Pages_Needed,Pages_Number_String);
  378. If Error_Code<>STATUS_OK then
  379. Error('EMS test program failed trying to allocate '+Pages_Number_String+
  380. ' pages for usage.',Error_Code);
  381. Writeln(APPLICATION_PAGE_COUNT,
  382. ' EMS page(s) allocated for the EMS test program.');
  383. Writeln;
  384.  
  385. { Map in the required logical pages to the physical pages
  386. given to us, in this case just one page                     }
  387. Logical_Page :=0;
  388. Physical_Page:=0;
  389. Error_Code:=
  390. Map_Expanded_Memory_Pages(
  391. Emm_Handle,Logical_Page,Physical_Page);
  392. If Error_Code<>STATUS_OK then
  393. Error('EMS test program failed trying to map '+
  394. 'logical pages onto physical pages.',Error_Code);
  395.  
  396. Writeln('Logical Page ',Logical_Page,
  397. ' successfully mapped onto Physical Page ',
  398. Physical_Page);
  399. Writeln;
  400.  
  401. { Get the expanded memory page frame address }
  402. Error_Code:= Get_Page_Frame_Base_Address(Page_Frame_Base_Address);
  403. If Error_Code<>STATUS_OK then
  404. Error('EMS test program unable to get the base Page'+
  405. ' Frame Address.',Error_Code);
  406. Writeln('The base address of the EMS page frame is - '+
  407. Hex_String(Page_Frame_Base_Address) );
  408. Writeln;
  409.  
  410. { Write a test pattern to expanded memory }
  411. For Offset:=0 to 16382 do
  412. Mem[Page_Frame_Base_Address:Offset]:=Offset mod 256;
  413.  
  414. { Make sure that what is in EMS memory is what we just wrote }
  415. Writeln('Testing EMS memory.');
  416.  
  417. Offset:=1;
  418. Verify:=True;
  419. while (Offset<=16382) and (Verify=True) do
  420. Begin
  421. If Mem[Page_Frame_Base_Address:Offset]<>Offset mod 256 then
  422. Verify:=False;
  423. Offset:=Succ(Offset);
  424. end;  { while (Offset<=16382) and (Verify=True) }
  425.  
  426. { If it isn't report the error }
  427. If not Verify then
  428. Error('What was written to EMS memory was not found during '+
  429. 'memory verification  test.',0);
  430. Writeln('EMS memory test successful.');
  431. Writeln;
  432.  
  433. { Return the expanded memory pages given to us back to the
  434. EMS memory pool before terminating our test program         }
  435. Error_Code:=Deallocate_Expanded_Memory_Pages(Emm_Handle);
  436. If Error_Code<>STATUS_OK then
  437. Error('EMS test program was unable to deallocate '+
  438. 'the EMS pages in use.',Error_Code);
  439. Writeln(APPLICATION_PAGE_COUNT,
  440. ' page(s) deallocated.');
  441. Writeln;
  442. Writeln('EMS test program completed.');
  443. end.
  444.